<style>
  .header-font {
    font-size: 15px;
    font-weight: bold;
  }
  .line {
    margin: 10px 0;
    width: 100%;
    border: 0.5px solid rgb(218, 218, 218);
  }
  .table {
    margin-top: 10px;
  }
  .task-form {
    margin-bottom: -10px;
  }
  .paging {
    float:right;
    margin-top:10px;
  }
</style>
<template>
  <div>
    <div class='header-font'>
      <Icon type="md-apps" />
      <span>任务数据</span>
    </div>
    <div class="line"></div>
    <task-list-form
      :title="title"
      class="task-form"
      @on-success-valid="handleSubmit">
    </task-list-form>
    <!-- <Button
      icon="md-add"
      type="primary"
      @click="handleClick">
      添加任务
    </Button> -->
    <Table
      class="table"
      :columns="columns"
      :data="historyData"
      :loading="loading"
      border>
    </Table>
    <table-page
      class="paging"
      :dataCount="dataCount"
      @change-page="changePage"
      @change-page-size="changePageSize"
      :pageSize="pageSize">
    </table-page>
  </div>
</template>
<script>
import {
  mapActions,
  mapState
} from 'vuex'
import TaskListForm from '_c/task-list-form'
import TablePage from '_c/table-page'
export default {
  components: {
    TaskListForm,
    TablePage
    // BtnExport,
    // BtnUpload
  },
  data () {
    return {
      page: 1,
      rows: 20,
      playload: null,
      title: '任务管理任务数据',
      loading: true,
      columns: [
        {
          title: '序号',
          type: 'index2',
          width: 100,
          align: 'center',
          render: (h, params) => {
            return h('span', params.index + (this.page - 1) * this.pageSize + 1)
          }
        },
        {
          title: '任务名称',
          key: 'name',
          align: 'center',
          minWidth: 180,
          render: (h, params) => {
            if (params.row.name && params.row.name.length > 8) {
              return h('span', {
                attrs: {
                  title: params.row.name
                }
              }, params.row.name ? params.row.name.slice(0, 8) + '...' : '-')
            } else {
              return h('span', params.row.name ? params.row.name : '-')
            }
          }
        },
        {
          title: '关联内容标题',
          key: 'cname',
          align: 'center',
          minWidth: 180,
          render: (h, params) => {
            if (params.row.cname && params.row.cname.length > 8) {
              return h('span', {
                attrs: {
                  title: params.row.cname
                }
              }, params.row.cname ? params.row.cname.slice(0, 8) + '...' : '-')
            } else {
              return h('span', params.row.cname ? params.row.cname : '-')
            }
          }
        },
        {
          title: '结算方式',
          key: 'method',
          minWidth: 180,
          align: 'center',
          render: (h, params) => {
            return h(
              'span',
              {},
              params.row.type === '3' ? '口碑结算' : params.row.type === '1' ? '到店结算' : params.row.type === '2' ? '成交结算' : params.row.type === '4' ? '销售素材' : '-'
            )
          }
        },
        {
          title: '任务设置积分',
          key: 'taskmoney',
          align: 'center',
          width: 180,
          render: (h, params) => {
            return h(
              'span',
              {},
              params.row.type === '3' ? `${params.row.permoney}分` : params.row.type === '1' ? `${params.row.budget}分` : params.row.type === '2' ? `${params.row.perrate}%` : '-'
            )
          }
        },
        {
          title: '创建时间',
          key: 'ctime',
          minWidth: 180,
          align: 'center',
          render: (h, params) => {
            return h('span', params.row.ctime)
          }
        },
        {
          title: '活动时间',
          key: 'time',
          minWidth: 200,
          align: 'center',
          render: (h, params) => {
            return h('span', `${this.$moment(params.row.btime).format('YYYY/MM/DD')} ~ ${this.$moment(params.row.etime).format('YYYY/MM/DD')}`)
          }
        },
        {
          title: '阅读量',
          key: 'readcnt',
          width: 100,
          align: 'center'
        },
        {
          title: '签约量',
          key: 'num',
          align: 'center',
          minWidth: 150
          // render: (h, params) => {
          //   return h(
          //     'span',
          //     {},
          //     // params.row.method === '0' ? '口碑结算' :  params.row.method === '1' ? '到店结算' : params.row.method === '2' ? '成交结算' : '-'
          //     `${params.row.receive} / ${params.row.mouth} / ${params.row.num}`
          //   )
          // }
        },
        {
          title: '目前佣金总额(元)',
          key: 'price',
          align: 'center',
          minWidth: 176,
          render: (h, params) => {
            return h(
              'span',
              {},
              params.row.price !== null ? params.row.price : '-'
            )
          }
        }
        // {
        //   title: '操作',
        //   key: 'cz',
        //   width: 246,
        //   align: 'center',
        //   render: (h, params) => {
        //     return h('div', [
        //       h('Button', {
        //         props: {
        //           type: 'primary',
        //           size: 'small',
        //           ghost: 'ghost'
        //         },
        //         style: {
        //           marginRight: '5px'
        //         },
        //         on: {
        //           click: () => {
        //             this.handleClick(params.row.id, params.row)
        //           }
        //         }
        //       }, '编辑')
        //     ])
        //   }
        // }
      ]
    }
  },
  computed: {
    ...mapState({
      taskData: state => state.taskData.taskData,
      dataCount: state => state.taskData.taskDataDataCount,
      historyData: state => state.taskData.taskDataHistoryData,
      pageSize: state => state.taskData.taskDataPageSize
    })
  },
  methods: {
    ...mapActions([
      'getTaskData',
      'getTaskDataCurrentRow',
      'editTaskData',
      'updateModalState',
      'updateConfirmModalState',
      'changeTaskDataPage',
      'changeTaskDataPageSize'
    ]),
    handleSubmit (playload) {
      this.loading = true
      this.playload = playload
      this.getTaskData(playload).then((res) => {
        if (res.data.state === '000') {
          this.loading = false
        }
      })
    },
    // handleClick () {
    //   let newRow = {}
    //   this.id = id
    //   if (row) {
    //     newRow = Object.assign({}, row)
    //   }
    //   this.getTaskListCurrentRow(newRow)
    //   this.updateModalState(true)
    // },
    changePage (index) {
      this.loading = true
      this.page = index
      this.changeTaskDataPage(index)
      this.getTaskData({
        page: index,
        rows: this.rows,
        ...this.playload
      }).then((res) => {
        if (res.data.state === '000') {
          this.loading = false
        }
      })
    },
    changePageSize (value) {
      this.loading = true
      this.rows = value
      this.changeTaskDataPageSize(value)
      this.changePage(this.page)
      this.getTaskData({
        rows: value,
        ...this.playload
      }).then((res) => {
        if (res.data.state === '000') {
          this.loading = false
        }
      })
    }
  },
  created () {
    this.getTaskData({}).then((res) => {
      if (res.data.state === '000') {
        this.loading = false
      }
    })
  }
}
</script>
